package top.ahwyy.controller.Action;

import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import top.ahwyy.utils.checkCodeArray;
import top.ahwyy.utils.checkCodeImage;

public class checkCodeAction implements ServletRequestAware,ServletResponseAware {
	
	public HttpServletRequest request;
	public HttpServletResponse response;
	public String time;
	
	public String execute(){
		String[] str = checkCodeArray.getACheckCode();
		
		//System.out.println(str[0]+str[1]+str[2]+str[3]);
		// session 验证 验证码
		HttpSession session = request.getSession(true);
		session.setAttribute("checkCode", str[0]+str[1]+str[2]+str[3]);
		
		//设置验证码过期时间
		
		//得到图片
        try {
			ImageIO.write(checkCodeImage.getACheckCodeImage(str), "jpg", response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return null;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response = arg0;
	}
}
